package com.db.mapper;

import com.db.pojo.Associator;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

import static com.graduate.utils.ConstantConfig.*;

/**
 * 文章操作操作Mapper
 *
 * @createtime 2020-04-07
 * @creator JingChu
 * @文章模块功能梳理： 查询文章列表, 文章详情, 发布文章, 修改文章信息(匿名, 不匿名),
 */
@Mapper
public interface ArticleMapper {

    //查询文章列表
    @Select("SELECT a.id id,a.title title,a.content content,p.id pic FROM article a LEFT JOIN picture p ON  " +
            "p.relative=a.id AND p.state='" + STATE_NO_DELETE + "' AND p.type='" + PICTURE_HEAD + "' WHERE  a.content_type='" +
            PICTURE_HEAD + "' AND a.state='" + STATE_NO_DELETE + "' AND a.status='" + ARTICLE_COVER_REVIEW + "' AND a.type=#{type}" +
            " ORDERY BY a.createtime DESC LIMIT #{limit} OFFSET #{offset}")
    List<Map<String, Object>> getArticleList(@Param("type") String type, @Param("limit") String limit, @Param("offset") String offset);

    //查询文章详情
    @Select("SELECT a.*,p.id FROM article a LEFT JOIN picture p ON  p.relative=a.id AND p.state='" + STATE_NO_DELETE +
            "'AND p.type='" + PICTURE_CONTENT + "' WHERE id=#{id}")
    Map<String, Object> getArticleDetail(@Param("id") String id);

    @Insert("INSERT INTO article (id,openid) VALUE (#{id},#{openid})")
    int publishArtilce(@Param("id") String id, @Param("openid") String openid);

    @Update("UPDATE associator SET ${setSql} WHERE id=#{id}")
    int update(@Param("setSql") String setSql, @Param("id") String id);

    @Select("SELECT * FROM associator WHERE openid=#{openid}")
    Associator getAssociatorByOpenid(@Param("openid") String openid);
}
